﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.ParkManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.ParkManage
{
    public class DeviceListServeice:IDeviceListServeice
    {
        private readonly IRepository<DeviceList> _deviceListServeice;

        public DeviceListServeice(IRepository<DeviceList> deviceListServeice)
        {
            _deviceListServeice = deviceListServeice;
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _deviceListServeice.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fyDeviceList"></param>
        /// <returns></returns>
        public async Task<Response<List<DeviceList>>> GetDeviceListAsync(FyDeviceList fyDeviceList)
        {
            var deviceList = await _deviceListServeice.GetAllListAsync();
            var result = deviceList.MapToList<DeviceList>();

            if (!string.IsNullOrEmpty(fyDeviceList.name))
            {
                result = result.Where(a => a.Name.Contains(fyDeviceList.name)).ToList();
            }

            if (!string.IsNullOrEmpty(fyDeviceList.state))
            {
                if (fyDeviceList.state == "true")
                {
                    result = result.Where(x => x.State == true).ToList();
                }
                else
                {
                    result = result.Where(x => x.State == false).ToList();
                }
            }

            var totalCount = result.Count();

            result = result.Skip(((fyDeviceList.pageIndex - 1) * fyDeviceList.pageSize)).Take(fyDeviceList.pageSize).ToList();

            return Response<List<DeviceList>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<DeviceList> GetDeviceListById(DeviceList input)
        {
            var equipment = _deviceListServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = equipment.MapTo<DeviceList>();
            return Response<DeviceList>.Ok(result);
        }

        /// <summary>
        /// 分页
        /// </summary>
        public class FyDeviceList
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? name { get; set; }
            public string? state { get; set; }
        }
    }
}
